package com.roomaja.roomaja.service;

import com.github.pagehelper.PageHelper;
import com.roomaja.roomaja.entity.WenKuEntity;
import com.roomaja.roomaja.entity.WenKuLabelEntity;
import com.roomaja.roomaja.mapper.WenKuEntityMapper;
import com.roomaja.roomaja.mapper.WenKuLabelEntityMapper;
import com.roomaja.roomaja.tool.PageBean;
import com.roomaja.roomaja.tool.WenKuTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

/**
 * 百度文库资源抽取逻辑实现类
 */
@Service
public class WenKuPageProcessor implements PageProcessor {

    @Autowired
    WenKuEntityMapper wenKuEntityMapper;
    @Autowired
    WenKuLabelEntityMapper wenKuLabelEntityMapper;

    // 部分一：抓取网站的相关配置，包括编码、抓取间隔、重试次数等
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Override
    // process是定制爬虫逻辑的核心接口，在这里编写抽取逻辑
    public void process(Page page) {
        List<String> urls = page.getHtml().links().regex("view/(.*).html").all();

        for(int i = 0; i < urls.size(); ++i) {
            if(this.wenKuEntityMapper.isExistByDocId(urls.get(i)) == 0) {
                urls.set(i, "https://wenku.baidu.com/view/" + urls.get(i) + ".html?from=search");

                //初始化文档对象
                WenKuEntity wenKuEntity = WenKuTool.initEntity(urls.get(i));

                if(wenKuEntity.getDocLabel() != null) {

                    //处理文档标签
                    StringBuffer wenkuLabel = new StringBuffer();
                    String[] labels = wenKuEntity.getDocLabel().split("-");

                    if(wenKuEntity.getDocLabel() != null){
                        for (String string:wenKuEntity.getDocLabel().split("-")) {
                            WenKuLabelEntity wenKuLabelEntity = wenKuLabelEntityMapper.findByLabelId(string);
                            if (wenKuLabelEntity != null){
                                //标签库存在标签名
                                wenkuLabel.append(wenKuLabelEntity.getLabelName());
                                wenkuLabel.append("-");
                            }else{
                                //标签库不存在标签名
                                int newLableId = Integer.valueOf(string);
                                String newLabelName = WenKuTool.getDocLabel(newLableId);

                                //存储新的标签信息
                                wenKuLabelEntityMapper.insert(new WenKuLabelEntity(newLableId, newLabelName));

                                wenkuLabel.append(newLabelName);
                                wenkuLabel.append("-");
                            }
                        }
                        wenKuEntity.setDocLabel(wenkuLabel.substring(0, wenkuLabel.length()-1));
                    }

                    //存储文档数据
                    wenKuEntityMapper.insert(wenKuEntity);
                }
            }
        }

        System.out.println(urls.size());
        //当前url
        String currentUrl = page.getUrl().get();
        //待处理url集合
        List<String> pendingUrls = page.getHtml().$("a", "href").all();

        for(int i = 0; i < pendingUrls.size(); ++i) {
            if(!(pendingUrls.get(i)).startsWith("http")) {
                if((pendingUrls.get(i)).startsWith("//")) {
                    pendingUrls.set(i, "https:" + pendingUrls.get(i));
                } else if((pendingUrls.get(i)).startsWith("/")) {
                    pendingUrls.set(i, "http://wenku.baidu.com" + pendingUrls.get(i));
                } else if((pendingUrls.get(i)).indexOf("javascript") == -1 && (pendingUrls.get(i)).indexOf("void(0)") == -1) {
                    if(currentUrl.indexOf("?") != -1 && (pendingUrls.get(i)).indexOf("?") != -1) {
                        pendingUrls.set(i, currentUrl.substring(0, currentUrl.indexOf("?") - 1) + pendingUrls.get(i));
                    } else {
                        pendingUrls.set(i, currentUrl + pendingUrls.get(i));
                    }
                } else {
                    pendingUrls.set(i, "");
                }
            }

            if((pendingUrls.get(i)).split("wenku").length > 2) {
                pendingUrls.set(i, "");
            }

            if(!(pendingUrls.get(i)).startsWith("https://wenku.baidu.com")) {
                pendingUrls.set(i, "");
            }

            if((pendingUrls.get(i)).indexOf("#") != -1) {
                pendingUrls.set(i, "");
            }
        }

        page.addTargetRequests(pendingUrls);
    }

    @Override
    public Site getSite() {
        return site;
    }

    /**
     * 分页查询
     * @param currentPage
     * @param doc_title
     * @return
     */
    public PageBean<WenKuEntity> getDocumentsByPage(int currentPage, String doc_title){
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        int pageSize = 10;
        PageHelper.startPage(currentPage, pageSize);

        List<WenKuEntity> allDocuments = wenKuEntityMapper.getWenkuList(doc_title);        // 全部文档
        int countNums = wenKuEntityMapper.countWenKuList(doc_title);            //总记录数
        PageBean<WenKuEntity> pageData = new PageBean<>(currentPage, pageSize, countNums);
        pageData.setDocuments(allDocuments);
        pageData.getTotalPage();
        return pageData;
    }

    /**
     * 判断文件是否已经下载过
     * @param doc_id
     */
    public boolean isEverDownload(String doc_id){
        int isEver =  wenKuEntityMapper.isEverDownload(doc_id);
        System.out.println(isEver);
        if (isEver == 0){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 获取文档详细信息
     * @param doc_id
     * @return
     */
    public WenKuEntity getDetailByDocId(String doc_id) {
        List<WenKuEntity> list = wenKuEntityMapper.getDetailByDocId(doc_id);
        if (list.size() > 0) {
            return list.get(0);
        }else {
            return null;
        }
    }
}
